package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.GoodsListAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.internal.component.DaggerCollectedGoodsComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.CollectedGoodsModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.GoodsModel;
import com.zw.yzk.kabu.presenter.CollectedGoodsPresenter;
import com.zw.yzk.kabu.view.view.CollectedGoodsView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/9.
 */

public class CollectedGoodsActivity extends BaseActivity implements CollectedGoodsView {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.goods_list)
    RecyclerView goodsList;
    @Bind(R.id.empty)
    View empty;

    @Inject
    CollectedGoodsPresenter presenter;
    private GoodsListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_collected_goods;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.collected_goods))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerCollectedGoodsComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .collectedGoodsModule(new CollectedGoodsModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
        getCollectedGoods(0);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getCollectedGoodsSuccess(List<GoodsModel> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
        refreshEmptyStatus(adapter.getItemCount() == 0);
    }

    @Override
    public void getCollectedGoodsFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        goodsList.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
        adapter = new GoodsListAdapter(null);
        goodsList.setAdapter(adapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getCollectedGoods(0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getCollectedGoods(adapter.getItemCount());
                    }
                }).build();
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取收藏的列表
     *
     * @param start 开始索引
     */
    private void getCollectedGoods(int start) {
        presenter.getCollectedList(start);
    }
}
